-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Don't allow {} to refer to implicit captures in format_args. #93394
Conversation
r? @estebank (rust-highfive has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
This should imho probably also add an explicit test for the |
@@ -369,7 +377,7 @@ impl<'a, 'b> Context<'a, 'b> { | |||
|
|||
let count = self.pieces.len() | |||
+ self.arg_with_formatting.iter().filter(|fmt| fmt.precision_span.is_some()).count(); | |||
if self.names.is_empty() && !numbered_position_args && count != self.args.len() { | |||
if self.names.is_empty() && !numbered_position_args && count != self.num_args() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Normally this (positive) branch would result in a message 1 positional argument in format string, but no arguments were given
, whereas now adding an implicit capture gets invalid reference to positional argument 0 (no arguments were given)
. Ideally this wording would be kept, even with implicit args; I think the proper check would then be self.names.len() == self.num_captured_args && !numbered_position_args && count != self.num_args()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then the let count =
above would also have to be adjusted to exclude all placeholders that refer to captures, which is a bit more complicated.
I agree the diagnostics here can be improved when there's captured arguments involved, but I rather leave that to a separate change that can fix all those cases properly.
Marked as beta-accepted per discussion in the libs api team just now. |
r=me, but we really want a crater run of this to make sure we're not regressing here and am slightly uncomfortable with a beta backport (but it's not my call) |
error: invalid reference to positional argument 0 (no arguments were given) | ||
--> $DIR/format-args-capture-issue-93378.rs:9:23 | ||
| | ||
LL | println!("{a:.n$} {b:.*}"); | ||
| ------- ^^^--^ | ||
| | | | ||
| | this precision flag adds an extra required argument at position 0, which is why there are 3 arguments expected |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This output needs some work to make it clearer :(
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, there's quite a few cases right now where this macro produces suboptimal diagnostics.
We have plans to change how fmt::Arguments
is implemented/represented, which would require a lot of changes to the format_args
builtin macro. That would probably be the best time to just rewrite the macro and the way it produces diagnostics.
We could run this PR through crater, but I suppose the beta crater run is good enough to catch any problems. |
Looks like the crater queue is almost empty, so we can quickly run this through crater to be sure: @craterbot check |
🚨 Error: missing start toolchain 🆘 If you have any trouble with Crater please ping |
@bors try |
⌛ Trying commit cef9b47 with merge 1b0139f5bdae7328e6d232947a8112333bb44eeb... |
I was told crater needs some maintenance after the current experiment finishes, so let's hold off until that's done. @pietroalbini can you start a |
☀️ Try build successful - checks-actions |
🚧 Experiment ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more |
🎉 Experiment
|
Just two failures in the crater run. Both of them are unrelated: a build script failing to download some file. @bors r=estebank |
📌 Commit cef9b47 has been approved by |
Don't allow {} to refer to implicit captures in format_args. Fixes rust-lang#93378
Rollup of 13 pull requests Successful merges: - rust-lang#88313 (Make the pre-commit script pre-push instead) - rust-lang#91530 (Suggest 1-tuple parentheses on exprs without existing parens) - rust-lang#92724 (Cleanup c_str.rs) - rust-lang#93208 (Impl {Add,Sub,Mul,Div,Rem,BitXor,BitOr,BitAnd}Assign<$t> for Wrapping<$t> for rust 1.60.0) - rust-lang#93394 (Don't allow {} to refer to implicit captures in format_args.) - rust-lang#93416 (remove `allow_fail` test flag) - rust-lang#93487 (Fix linking stage1 toolchain in `./x.py setup`) - rust-lang#93673 (Linkify sidebar headings for sibling items) - rust-lang#93680 (Drop json::from_reader) - rust-lang#93682 (Update tracking issue for `const_fn_trait_bound`) - rust-lang#93722 (Use shallow clones for submodules managed by rustbuild, not just bootstrap.py) - rust-lang#93723 (Rerun bootstrap's build script when RUSTC changes) - rust-lang#93737 (bootstrap: prefer using '--config' over 'RUST_BOOTSTRAP_CONFIG') Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
Don't allow {} to refer to implicit captures in format_args. Fixes rust-lang#93378
[beta] Backports Backports of: * rust-lang#92611 — Add links to the reference and rust by example for asm! docs and lints * rust-lang#92983 — Update Linux runners to Ubuntu 20.04 * rust-lang#93081 — Update LLVM submodule * rust-lang#93394 — Don't allow {} to refer to implicit captures in format_args. * Cargo: * rust-lang/cargo#10377 — Remove strip = "off" (and undocumented strip = "n"/strip = "no")
Fixes #93378